﻿using System;
using System.Collections.Generic;
using System.Linq;
using Pharmacy.Data;
using Pharmacy.Data.Entity;
using System.Transactions;
using Pharmacy.Data.Repositories;
using Pharmacy.Data.Core;
using Pharmacy.Ultilities.Resources;
using Pharmacy.Ultilities;


namespace Pharmacy.Business
{
    public partial class MasterItemGroupBO : BaseBusiness
    {
        #region Constructors
        public MasterItemGroupBO(IObjectContext objectContext) : base(objectContext) { }
        public MasterItemGroupBO() : base() { }
        #endregion

        #region Public Methods
        public MasterItemGroup CreateNewMasterItemGroup(string name, string note)
        {
            MasterItemGroup newObject;
            using (TransactionScope scope = new TransactionScope())
            {
                newObject = new MasterItemGroup();
                newObject.Name = name;
                newObject.Note = note;
                MasterItemGroupRepository lst = new MasterItemGroupRepository(PharmacyContext);
                lst.Add(newObject);
                PharmacyContext.SaveChanges();
                scope.Complete();
            }
            return newObject;
        }

        public bool EditMasterItemGroup(int id, string name, string note, out string messageError)
        {
            MasterItemGroup editObject = null;
            MasterItemGroupRepository lst = new MasterItemGroupRepository(PharmacyContext);
            editObject = lst.AsQueryable().First(tmp => tmp.ID == id);
            if (editObject != null)
            {
                messageError = editObject.IsCheckOutByAnotherUser();
                if (messageError.Length == 0)
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        try
                        {
                            LockingControl.Lock(editObject);
                            editObject.Name = name;
                            editObject.Note = note;
                            PharmacyContext.SaveChanges();
                            scope.Complete();
                            LockingControl.UnLock(editObject);
                            return true;
                        }
                        catch (PessimisticLockingException ex)
                        {
                            throw new PessimisticLockingException(
                                string.Format(SR.Instance.GetString(Constant.MSG_LOCKBYANOTHERUSER),
                                SR.Instance.GetString(Constant.ENT_MASTERITEMGROUP), editObject.Name),
                                ex.LockDataInfo);
                        }
                    }
                }
            }
            else
            {
                messageError = string.Format(SR.Instance.GetString(Constant.KEY_OBJECTISDELETED),
                    Constant.ENT_MASTERITEMGROUP);
            }
            return false;
        }

        public bool DeleteMasterItemGroup(int id, out string messageError)
        {
            MasterItemGroup deleteObject = null;
            MasterItemGroupRepository lst = new MasterItemGroupRepository(PharmacyContext);
            deleteObject = lst.AsQueryable().First(tmp => tmp.ID == id);
            messageError = string.Empty;
            if (deleteObject != null)
            {
                messageError = (LockingControl as PessimisticLocking).IsCheckOutByAnotherUser(deleteObject);
                if (messageError.Length == 0)
                {
                    using (TransactionScope scope = new TransactionScope())
                    {
                        try
                        {
                            LockingControl.Lock(deleteObject);
                            lst.Delete(deleteObject);
                            PharmacyContext.SaveChanges();
                            scope.Complete();
                            LockingControl.UnLock(deleteObject);
                            return true;
                        }
                        catch (PessimisticLockingException ex)
                        {
                            throw new PessimisticLockingException(
                                string.Format(SR.Instance.GetString(Constant.MSG_LOCKBYANOTHERUSER),
                                SR.Instance.GetString(Constant.ENT_MASTERITEMGROUP), deleteObject.Name),
                                ex.LockDataInfo);
                        }
                    }
                }
            }
            else
            {
                messageError = string.Format(SR.Instance.GetString(Constant.KEY_OBJECTISDELETED),
                    Constant.ENT_MASTERITEMGROUP);
            }

            return false;
        }
        #endregion
    }
}
